/**
 * <p>Copyright (c) 2017 深圳市鹏途交通科技有限公司 </p>
 * <p>				   All right reserved. 		     </p>
 * 
 * <p>项目名称 ： 	深圳市国省检日常化监管系统         </p>
 * <p>创建者   :	hey 
 * 
 * <p>描   述  :   BridgeDetectionServiceImpl.java for com.pengtu.gsj.service.impl.check.yh.bridgeTunnel    </p>
 * 
 * <p>最后修改 : $: 2017年8月17日-上午11:37:16 v 1.0.0	 hey   $ </p>
 * 
*/
package com.pengtu.gsj.service.impl.check.yh.bridgeTunnel;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.alibaba.fastjson.JSONObject;
import com.pengtu.gsj.entity.app.Attachment;
import com.pengtu.gsj.entity.check.CheckTable;
import com.pengtu.gsj.service.check.CheckTableService;
import com.pengtu.gsj.service.check.yh.bridgeTunnel.BridgeDetectionService;
import com.pengtu.gsj.service.system.SystemManageService;
import com.pengtu.gsj.utils.DateUtils;
import com.pengtu.gsj.utils.MathsUtils;

/**
 * 
 * BridgeDetectionServiceImpl
 * 
 * 2017年8月17日 上午11:37:16
 * 
 * @version 1.0.0
 * 
 */
@Service
@Transactional
public class BridgeDetectionServiceImpl implements BridgeDetectionService{
	@Autowired
	private CheckTableService checkTableService;
	
	@Autowired
	private SystemManageService systemManageService;
	
	
	
	@Override
	public Map<String, Object> getDevelopPlanmMap(String startTime, String endTime, String type, CheckTable checkTable, String tag)throws Exception{
		Map<String, Object> map = new HashMap<String, Object>();
		
		JSONObject jsonObject = JSONObject.parseObject(checkTable.getRetrieval());
		double yearFull = Double.parseDouble((String)jsonObject.get("yearFull"));
		//取到所有文件
		List<Attachment> list = systemManageService.getAllCheckAttachments(startTime, endTime, tag);
		//获取隧道维护文件
		List<Attachment> whlist = new ArrayList<Attachment>();
		//0表示国检，1表示省检
		if ("0".equals(type)) {
			whlist = systemManageService.getCheckAttachmentByTag(String.valueOf(Integer.valueOf(startTime)+1), String.valueOf(Integer.valueOf(startTime)+2), "qljczg_qlzzwh");
		} else if ("1".equals(type)) {
			whlist = systemManageService.getCheckAttachmentByTag(startTime, startTime, "qljczg_qlzzwh");
		}
		double total =0; 				//检查得分
		double percentage =0; 			//系统完善度
		double tableFull  = 0;			//满分
		int years[] = null;
		if ("0".equals(type)) {
			tableFull = yearFull *2;
			years = new int[2];
			years[0] = Integer.parseInt(startTime)+1;
			years[1] = Integer.parseInt(startTime)+2;
		}else {
			tableFull = yearFull;
			years = new int[1];
			years[0] = Integer.parseInt(startTime);
		}
		List<String> checkList = Arrays.asList("0", "0");
		
		//判断每年是否有管理目标文件
		for (int i=0; i<years.length; i++) {
			for (int j = 0; j< whlist.size(); j++) {
				if (years[i] == Integer.parseInt(DateUtils.convertDateToString("yyyy", whlist.get(j).getFileTime()))) {
					checkList.set(i, "1");
					break;
				}
			}
		}
		
		for (int i = 0; i < checkList.size(); i++) {
			if ("1".equals(checkList.get(i))) {
				total += yearFull;
			}
		}
		
		map.put("sum",  MathsUtils.formatDouble(total,2));
		//判断完善度
		percentage = MathsUtils.divDouble(total *100 , tableFull, 2) ;
		checkTableService.updateCheckTable(checkTable, percentage, total, tableFull, true);
		map.put("percent", percentage);
		map.put("years", years);
		map.put("att", list);
		map.put("type", type);
		map.put("whlist", whlist);
		map.put("score", checkTable.getTestFull());
		map.put("checkTable", checkTable);
		
		return map;
	}
}
